<template>
  <div>
    <!-- 用了一个行列布局 -->
    <el-row
      type="flex"
      justify="space-between"
      align="middle"
    >
      <el-col>
        <strong v-if="istool">{{ treeNode.name }}</strong>
        <span v-else>{{ treeNode.name }}</span>
      </el-col>
      <el-col :span="4">
        <el-row
          type="flex"
          justify="end"
        >
          <!-- 两个内容 -->
          <el-col>{{ treeNode.manager }}</el-col>
          <el-col>
            <!-- 下拉菜单 element -->
            <el-dropdown @command="hDelCommand">
              <span>
                操作<i class="el-icon-arrow-down" />
              </span>
              <!-- 下拉菜单 -->
              <!-- <el-dropdown-menu
                v-if="istool"
                slot="dropdown"
              >
                <el-dropdown-item>添加子部门</el-dropdown-item>
              </el-dropdown-menu> -->
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item
                  v-if="!istool"
                  command="edit"
                >编辑子部门</el-dropdown-item>
                <el-dropdown-item
                  v-if="!istool"
                  command="del"
                >删除子部门</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object,
      require: true
    },
    istool: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async hDelCommand (type) {
      // console.log(123)
      if (type === 'add') {
        this.$emit('add-depts', this.treeNode)
        // console.log(type)
      } else if (type === 'edit') {
        // console.log(type)
        this.$emit('edit-depts', this.treeNode)
        console.log(this.treeNode)
      } else if (type === 'del') {
        try {
          // 1、触发提示确认是否删除？
          await this.$confirm('确认删除此部门')
          // console.log(type)
          // 2、调用删除接口
          await delDepartments(this.treeNode.id)
          // 3、提示删除成功
          this.$message.success('删除成功')
          // 4、触发子传父更新数据
          this.$emit('update-departments')
        } catch (error) { // 使用try catch包裹，后台保存可以在控制台输出
          console.log(error)
        }
      }
    }
  }
}
</script>

